#include "ouden.h"

namespace ouden
{
	
	//Return the singleton instance of the engine
	ODN_Engine* getEngine()
	{
		return ODN_Engine::Instance();
	}
	
	//Initialise the engine with renderer settings 
	bool ODN_Engine::Init(char* Title, int Width, int Height, int BitsPerPixel, bool Fullscreen)
	{
		
		//Create the renderer window 
		renderer->CreateRendererWindow(Title, Width, Height, BitsPerPixel, Fullscreen);
		
		
		
	}
	
	//Run the main loop 
	void ODN_Engine::Run()
	{
		
		bool running = true;
		
		//Main loop 
	    while (running) {
			
			
			//Check for windows messages
			if (renderer->CheckMessages()) continue;
			
			//Make sure the renderer is currently active
			if (!renderer->isActive()) continue;
	
			//Check if the Engine has recieved a quit message
			if (renderer->isQuit()) running = false;
			
			//OpenGL error checking
			int r = glGetError();
			if (r != GL_NO_ERROR) { std::cout << "OpenGL Error " << r << std::endl; }
			
			//Retrieve input from input manager
			inputmanager->retrieveInput();
			
			//Check for escape key
			if (inputmanager->isKeyDown(VK_ESCAPE)) running = false;
			
			//Update the timer 
			timemanager->Update();
			
			//TODO: Look at this 
			timemanager->setElapsed(100);
			
			//Update the engine
			Update();
			
			//Draw a test marker 
			glColor4f(1, 1, 1, 1);
			glBegin(GL_POINTS);
				glVertex3f(0, 0, 0);
			glEnd();
			
			//Render the scene
			renderer->DrawScene(); 
			
		}
		
	}
	
	//Updates and draws the scene 
	bool ODN_Engine::Update()
	{
		scene->Update();
		scene->Draw();
	}
	
}
